package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.Material;
import org.linlinjava.litemall.db.domain.MaterialExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MaterialService {


    @Resource
    private MaterialMapper materialMapper;

    public int updateById(Material brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return materialMapper.updateByPrimaryKeySelective(brand);
    }

    public void deleteById(Integer id) {
        materialMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(Material brand) {
        LocalDateTime now = LocalDateTime.now();
        brand.setAddTime(now);
        brand.setUpdateTime(now);
        materialMapper.insertSelective(brand);
    }

    public List<Material> all() {
        MaterialExample example = new MaterialExample();
        example.or().andDeletedEqualTo(false);
        return materialMapper.selectByExample(example);
    }

    public List<Material> query(Integer page, Integer limit, String sort, String order) {
        MaterialExample example = new MaterialExample();
        example.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return materialMapper.selectByExample(example);
    }

    public List<Material> query(Integer page, Integer limit) {
        return query(page, limit, "add_time", "desc");
    }

    public Material findById(Integer id) {
        return materialMapper.selectByPrimaryKey(id);
    }

    public List<Material> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
        MaterialExample example = new MaterialExample();
        MaterialExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return materialMapper.selectByExample(example);
    }

    public List<Material> search(String keyword) {
        MaterialExample example = new MaterialExample();
        MaterialExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike("%" + keyword + "%");
        criteria.andDeletedEqualTo(false);
        return materialMapper.selectByExample(example);
    }

    public List<Material> queryByIds(List<Integer> materialIds) {
        MaterialExample example = new MaterialExample();
        MaterialExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(materialIds);
        criteria.andDeletedEqualTo(false);
        return materialMapper.selectByExample(example);
    }

    public Material getById(Integer materialId) {
        return materialMapper.selectByPrimaryKey(materialId);
    }

    public void addAll(List<Material> list) {
        if(!CollectionUtils.isEmpty(list)){
            for (Material material:list){
                materialMapper.insertSelective(material);
            }
        }

    }

    public Long count() {
        MaterialExample example = new MaterialExample();
        MaterialExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        return materialMapper.countByExample(example);
    }
}
